/*
   Copyright (C) 2009 Klaus Blindert <klaus.blindert@web.de>

   Part of the libABF project.

   libABF is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include "abf.h"

#include <stdio.h>
#include <string.h>

typedef struct
{
    const char* name;
    int (*command)(int,char**);
} command_t;

int command_dump(int argc, char** argv)
{
    int i;
    if (argc==0)
    {
        printf("Usage: abf-tool dump <filename> [<filename> ...]\n");
        return 1;
    }
    for (i=0; i!=argc; ++i)
    {
        abf_flat_t* flat = abf_create();
        abf_status status = abf_load (abf_getformat (ABF_F_ANY), flat, argv[0]);
        if (ABF_OK != status)
            return 1;
        abf_dump (flat, ABF_NOLIMIT);
        abf_destroy(flat);
    }
    return 0;
}

int command_toxml(int argc, char** argv)
{
    if (argc!=2)
    {
        printf("Usage: abf-tool toxml <input-filename> <output-filename>\n");
        return 1;
    }
    abf_flat_t* flat = abf_create();
    abf_status status = abf_load (abf_getformat (ABF_F_ANY), flat, argv[0]);
    if (ABF_OK != status)
        return 1;
    abf_save (abf_getformat (ABF_F_XML), flat, argv[1]);
    abf_destroy(flat);
    return 0;
}

int command_todat(int argc, char** argv)
{
    if (argc!=2)
    {
        printf("Usage: abf-tool todat <input-filename> <output-filename>\n");
        return 1;
    }
    abf_flat_t* flat = abf_create();
    abf_status status = abf_load (abf_getformat (ABF_F_ANY), flat, argv[0]);
    if (ABF_OK != status)
        return 1;
    abf_save (abf_getformat(ABF_F_DAT), flat, argv[1]);
    abf_destroy(flat);
    return 0;
}

command_t commands[] =
{
    { "dump", &command_dump },
    { "toxml", &command_toxml },
    { "todat", &command_todat },
};
unsigned n_commands = sizeof(commands)/sizeof(commands[0]);

void print_usage()
{
    int i;
    printf ("Usage: abf-tool <command> [arguments ...]\nList of available commands:\n");
    for (i=0; i < n_commands; ++i)
        printf("\t%s\n",commands[i].name);
}

int main(int argc, char** argv)
{
    int cmd_i;
    char* cmd;
    if (argc < 2)
    {
        print_usage();
        return 1;
    }
    cmd = argv[1];
    argv += 2;
    argc -= 2;

    for (cmd_i=0; cmd_i!=n_commands; ++cmd_i)
        if (strcmp(commands[cmd_i].name,cmd) == 0)
            break;

    if (cmd_i==n_commands)
    {
        print_usage();
        return 1;
    }

    return commands[cmd_i].command(argc,argv);
}